package Thread;

/**
 * 赖汉模式创建单例模式多线程
 */
class SingletonLazy{
    private static  volatile SingletonLazy instance = null;
    private static  Object locker = new Object();


    //外层的if就是判定下看当前是否已经把instance实例创建出来
    // 同时为了避免“内存可见性”导致的读取的instance出现偏差，于是补上volatile。
    public  static  SingletonLazy getInstance(){
        if (instance == null){ //判定是否需要加锁，
            synchronized (locker){
                if (instance==null){  //判定是否 new对象
                    instance = new SingletonLazy();
                }

            }
        }
        return instance;
    }
    private SingletonLazy(){

    }
}
public class Demo26 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1==s2);
    }
}
